/*
 * 描述 数据源配置接口
 * Create by cy on 2025/06/16
 * */

const express = require("express");
const router = express.Router();
const db = require("../../db");

router.get("/list", (req, res) => {
  let s = `SELECT * from express.data_source_configs`;

  db.query(s, function (error, result) {
    if (error) throw error;
    res.json({
      data: result,
    });
  });
});
//新增接口
router.post("/create", (req, res) => {
  const {
    ds_name,
    ds_code,
    ds_type,
    jdbc_url,
    username,
    password,
    status,
    created_by,
  } = req.body;
  const sql = `INSERT INTO data_source_configs 
    (ds_name, ds_code, ds_type, jdbc_url, username, password, status, created_by) 
    VALUES (?, ?, ?, ?, ?, ?, ?, ?)`;
  console.log(sql, req.body);
  db.query(
    sql,
    [
      ds_name,
      ds_code,
      ds_type,
      jdbc_url,
      username,
      password,
      status,
      created_by,
    ],
    (error, result) => {
      if (error) {
        console.log(error);
        res.status(500).json({ error: error.message });
        return;
      }
      res.json({ id: result.insertId });
    }
  );
});
//修改接口
router.post("/update", (req, res) => {
  const {
    id,
    ds_name,
    ds_code,
    ds_type,
    jdbc_url,
    username,
    password,
    status,
  } = req.body;
  const sql = `UPDATE data_source_configs SET ds_name=?, ds_code=?, ds_type=?, jdbc_url=?, username=?, password=?, status=? WHERE id=?`;
  db.query(
    sql,
    [ds_name, ds_code, ds_type, jdbc_url, username, password, status, id],
    (error, result) => {
      if (error) throw error;
      res.json({});
    }
  );
});
//删除接口
router.post("/delete", (req, res) => {
  const { id } = req.body;
  const sql = `DELETE FROM data_source_configs WHERE id=?`;
  db.query(sql, [id], (error, result) => {
    if (error) throw error;
    res.json({});
  });
});
module.exports = router;
